package com.binguo.api.service;

import com.binguo.api.response.shiyunji.JsonsRootBean;
import com.binguo.common.paymodel.HttpConnectionManager;
import com.binguo.common.util.JsonUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ShiYunJiService {

    /**
     * {"model":{"deliverFee":0.0,"group":[],"hasInvoiced":0,"isBook":0,
     * "isOnlinePaid":0,"orderId":"155861837710402748","orderPrice":0.0,
     * "orderTime":"2019-05-23 21:32:57","packageFee":0.0,"payType":10,
     * "platform":0,"printModel":0,"promotion":0.0,"promotionDoubleDiscountPOI":0.0,
     * "promotionDoubleDiscountSYJ":0.0,"promotionRedbag":0.0,"serialNumber":0,"status":0,
     * "statusCode":0,"totalPrice":1.0},"responseCode":"0","responseMsg":"商家接单成功"}
     */
    private static String url = "http://system.shiyunji.com/app-task.do";

    public static boolean doPay(String orderNo, String price, String authCode) {
        String storeId = "af5653d3f26d11e681dee6fd7d257a62"; //临时门店ID
        String shopId = "6db135480bd411e8840531c7544e89c6"; //临时商户ID
        Map<String, Object> param = new HashMap<>();
        param.put("deviceId", "xxxx"); //设备ID
        param.put("token", "TOKEN1234yepeng"); //写死token
        param.put("version", "1.0");
        param.put("appVersion", "1.0");
        param.put("bizCode", "A001");
        param.put("storeId", storeId);
        param.put("shopId", shopId);
        param.put("authCode", authCode);//识别码
        param.put("orderType", "15");//订单类型
        param.put("amount", price);//订单金额
        param.put("remark", "备注");//备注

        try {

            HttpPost httpPost = new HttpPost(url);

            String requestContent = JsonUtil.toJson(param);
            System.out.println("requestContent=" + requestContent);

            StringEntity stringEntity = new StringEntity(requestContent, ContentType.APPLICATION_JSON);
            httpPost.setEntity(stringEntity);
            CloseableHttpResponse closeableResponse = HttpConnectionManager.getHttpClient().execute(httpPost);
            String wxresp = EntityUtils.toString(closeableResponse.getEntity(), "utf-8");

            System.out.println("resp=" + wxresp);

            JsonsRootBean jsonsRootBean = JsonUtil.parse(wxresp, JsonsRootBean.class);

            if (jsonsRootBean.getResponsecode().equals("0")) {
                //success
                return true;
            }
        }catch (Exception e){


        }

        return false;
    }


}
